package com.chonry.web2;

import com.chonry.web2.filter.JwtFilter;
import com.chonry.web2.services.DemoService;
import io.shardingjdbc.spring.boot.SpringBootConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;

@SpringBootApplication(exclude = {
        DataSourceAutoConfiguration.class,
        SpringBootConfiguration.class
})
public class Web2Application {

    @Bean
    public FilterRegistrationBean jwtFilter() {
        FilterRegistrationBean rbean = new FilterRegistrationBean();
        rbean.setFilter(new JwtFilter());
        rbean.addUrlPatterns("/user/*");// 过滤的链接
        return rbean;
    }

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {

        return (container -> {
            ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500");
            container.addErrorPages(error500Page);
        });
    }

    public static void main(String[] args) {

        ApplicationContext applicationContext = SpringApplication.run(Web2Application.class, args);
        DemoService bean = applicationContext.getBean(DemoService.class);
        bean.insert();
        bean.insertSharding();
        bean.ids();
    }
}